package com.lh7.ledger.controller;

import com.lh7.ledger.dto.LedgerRecordRequest;
import com.lh7.ledger.service.*;
import com.lh7.ledger.utils.Result;
import com.lh7.ledger.vo.StructuredTableResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Comprehensive")
@CrossOrigin(origins ="*")
public class ComprehensiveController {
    @Autowired
    private IComprehensiveService comprehensiveService;

    @GetMapping("/selectLedgerById/{ledgerId}")
    public Result<StructuredTableResponse> selectAll(@PathVariable("ledgerId") Long ledgerId) {

        return Result.success(comprehensiveService.buildLedgerTable(ledgerId));
    }

    @GetMapping("/selectRecordById/{recordId}")
    public Result<StructuredTableResponse> selectRecordById(@PathVariable("recordId") Long recordId){
        return Result.success(comprehensiveService.buildLedgerRecord(recordId));
    }

    @PostMapping("/insertRecord")
    public Result<?> insertRecord(@RequestBody  LedgerRecordRequest recordRequest){
        int i = comprehensiveService.insertRecord(recordRequest);
        if (i > 0) {
            return Result.success("插入成功");
        }
        return Result.failure("插入失败");
    }

    @PutMapping("/updateRecord")
    public Result<?> updateRecord(@RequestBody LedgerRecordRequest recordRequest){
        int i = comprehensiveService.updateRecord(recordRequest);
        if (i > 0) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    @DeleteMapping("/deleteRecord/{recordId}")
    public Result<?> deleteRecord(@PathVariable("recordId") Long recordId){
        int i = comprehensiveService.deleteRecord(recordId);
        if (i > 0) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }
}
